module(...,package.seeall) require("xap") require("xap.bsc") require("string") require("pl") info={ version="H.K2.gdwmod2", description="Heating control" } t=pretty.read(file.read("/etc/www/heating.cfg")) function heatingcontrol() -- this function only called on 1wire temp change HTemp = xap.getValue("input.state","text") t=pretty.read(file.read("/etc/www/heating.cfg")) if (HTemp == "?") then HTemp = t.htemp --if no temp, use previous config setting else t.htemp = HTemp --if real temp received set to config file end if t.hstate == t.on then --turn on relay if temp too low and heating on but relay off if t.hrelay == t.off then if tonumber(HTemp) < tonumber(t.hthermostat)then bsc.sendState("dbzoo.livebox.Controller:relay.1","on") t.hrelay = t.on end end end if t.hrelay == t.on then --if relay on and temp too high turn relay off if tonumber(HTemp) > tonumber(t.hthermostat)then bsc.sendState("dbzoo.livebox.Controller:relay.1","off") t.hrelay = t.off end end file.write("/etc/www/heating.cfg",pretty.write(t)) end function thermostatsetting(endpoint) --called when thermostat endpoint changed if (endpoint.text == "?") then endpoint.text = t.hthermostat --if just started up, use previous config setting else t.hthermostat = tonumber(endpoint.text) --will bug out if text is not numeric, to do file.write("/etc/www/heating.cfg",pretty.write(t)) if t.hstate == t.on then --don't wait for 1wire, switch on relay if needed if tonumber(t.htemp) < tonumber(t.hthermostat)then bsc.sendState("dbzoo.livebox.Controller:relay.1","on") t.hrelay = t.on else bsc.sendState("dbzoo.livebox.Controller:relay.1","off") t.hrelay = t.off end end end file.write("/etc/www/heating.cfg",pretty.write(t)) end function hstate(endpoint) --called when heating endpoint changed t=pretty.read(file.read("/etc/www/heating.cfg")) t.hstate = endpoint.state if t.hstate == t.on then --don't wait for 1wire, switch on relay if needed if tonumber(t.htemp) < tonumber(t.hthermostat)then bsc.sendState("dbzoo.livebox.Controller:relay.1","on") t.hrelay = t.on end end if t.hstate == t.off then --don't wait for 1wire, switch off relay bsc.sendState("dbzoo.livebox.Controller:relay.1","off") t.hrelay = t.off end file.write("/etc/www/heating.cfg",pretty.write(t)) end function init() local fb = xap.Filter() fb:add("xap-header","source","dbzoo.livebox.Controller:1wire.5") fb:add("xap-header","class","xAPBSC.event") fb:callback(heatingcontrol) bsc.Endpoint{source="dbzoo.livebox.kema:Heating.Status", direction=bsc.OUTPUT, type=bsc.BINARY, cmdCB=hstate} bsc.Endpoint{source="dbzoo.livebox.kema:Heating.thermostat", direction=bsc.OUTPUT, type=bsc.STREAM, cmdCB=thermostatsetting} end